﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyPublisher.Composition
{
    public class Fragments : List<Fragment>
    {
        public T GetFragment<T>(FragmentType type) where T:Fragment
        {
            foreach (Fragment fragment in this)
            {
                if (fragment.Type == type)
                {
                    return fragment as T;
                }
            }

            return null;
        }

        public bool ContainFragment(FragmentType type)
        {
            foreach (Fragment fragment in this)
            {
                if (fragment.Type == type)
                {
                    return true;
                }
            }

            return false;
        }

        public string ToParameter()
        {
            StringBuilder sb = new StringBuilder();
            foreach (Fragment fragment in this)
            {
                sb.AppendFormat(
                    "-{0} \"{1}\" ",
                    fragment.Type.ToString().ToLower(),
                    fragment.ToString());
            }

            return sb.ToString().TrimEnd();
        }
    }
}
